//
//  CheckRelustViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/9.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "CheckRelustViewController.h"
#import "ResultPeopleTableViewCell.h"
#import "ResultShopInforTableViewCell.h"
#import "ResultScoreTableViewCell.h"
#import "ResultoptionTableViewCell.h"
#import "CheckResultModel.h"
#import "CheckUnPassViewController.h"
#import "RootNavigationController.h"
#import "MorePageSubViewController.h"
#import "SinglePageSubViewController.h"
#import "NewSubjectModel.h"
#import "PtPeopleView.h"
#import "DatePickView.h"
@interface CheckRelustViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView*hometableView;

@property(nonatomic,strong)CheckResultModel *ResultModel;

@end

@implementation CheckRelustViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"调查结果";

    [self.view addSubview:self.hometableView];
    

    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [self loadDatas];

}


-(TB_TableView*)hometableView
{
    if (!_hometableView) {
        
        _hometableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        
        _hometableView.delegate=self;
        
        _hometableView.dataSource=self;
        
        _hometableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        _hometableView.tableFooterView=[[UIView alloc]init];
        
        [_hometableView registerNib:[UINib nibWithNibName:@"ResultPeopleTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
        
          [_hometableView registerNib:[UINib nibWithNibName:@"ResultShopInforTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
        
          [_hometableView registerNib:[UINib nibWithNibName:@"ResultScoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell3"];
        
          [_hometableView registerNib:[UINib nibWithNibName:@"ResultoptionTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];
        
        [self configureNeedShowEmptyDataSetScrollView:_hometableView];
        
    }
    
    return _hometableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!_ResultModel) {
        
        return 0;
    }
    return 4;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        ResultPeopleTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        
        cell.CheckNameLabel.text=self.ResultModel.data.checkUserName;
        //确认状态
        cell.PeopleNameLabel.text = self.ResultModel.data.confirmState==YES?@"确认":@"未确认";
        
//        cell.PeopleNameLabel.text=self.ResultModel.data.juror.length==0?@"暂无":self.ResultModel.data.juror;
        
//        if (self.ResultModel.data.juror.length==0) {
//
//            cell.PeopleNameLabel.textColor=[UIColor lightGrayColor];
//        }else{
//
//            cell.PeopleNameLabel.textColor=[UIColor blackColor];
//        }
        
        cell.changeStatueLabel.text=self.ResultModel.data.rectifyState==YES?@"已整改":@"未整改";
        
        return cell;
        
        
    }else if (indexPath.section==1)
    {
        ResultShopInforTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
      
        cell.SubjectNameLabel.text=self.ResultModel.data.subjectName;
        cell.ShopNameLabel.text=self.ResultModel.data.shopName;
    
        cell.CheckDateLabel.text=[NSString stringWithFormat:@"检查日期:%@",self.ResultModel.data.checkDate];
   
        cell.ShouldChangeLabel.text=[NSString stringWithFormat:@"责令整改:%@",self.ResultModel.data.orderRectifyDate.length==0?@"暂无":self.ResultModel.data.orderRectifyDate];
        
        return cell;
        
        
    }else if (indexPath.section==2)
    {
        ResultScoreTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        
        //等级状态
        if(self.ResultModel.data.isSign == YES)
        {
            cell.ResultLabel.text=self.ResultModel.data.resultGraded;
            
            if ([self.ResultModel.data.resultGraded isEqualToString:@"A级"]) {
                
                
                cell.ResultImageView.image=[UIImage imageNamed:@"resultA"];
                
                cell.ResultLabel.textColor=kColorRGB(0x58be8b);
                
            }else if ([self.ResultModel.data.resultGraded isEqualToString:@"B级"])
            {
                cell.ResultImageView.image=[UIImage imageNamed:@"resultB"];
                cell.ResultLabel.textColor=kColorRGB(0xf9cd55);
                
            }else if ([self.ResultModel.data.resultGraded isEqualToString:@"C级"])
            {
                cell.ResultImageView.image=[UIImage imageNamed:@"resultC"];
                cell.ResultLabel.textColor=kColorRGB(0xf7707a);
                
            }else{
                
                cell.ResultImageView.image=[UIImage imageNamed:@"resultD"];
                
                cell.ResultLabel.textColor=kColorRGB(0xcccccc);
                
            }
        }else{
            //何波修改
            cell.ResultImageView.image=[UIImage imageNamed:@"checkGrade"];
        }
        
        cell.EntourageLabel.text = [NSString stringWithFormat:@"陪同人员:%@  陪同检查人员:%@",self.ResultModel.data.juror.length==0?@"暂无":self.ResultModel.data.juror,self.ResultModel.data.checkJuror.length==0?@"暂无":self.ResultModel.data.checkJuror];
        cell.EntourageLabel.textColor = kColorRGB(0xcccccc);
        
        cell.AllNumLabel.text=[NSString stringWithFormat:@"%ld",self.ResultModel.data.passNums+self.ResultModel.data.notPassNums+self.ResultModel.data.notSuitableNums];
        
        cell.PassNumLabel.text=[NSString stringWithFormat:@"%ld",self.ResultModel.data.passNums];
        
        cell.NotPassNumLabel.text=[NSString stringWithFormat:@"%ld",self.ResultModel.data.notPassNums];
        
        cell.NotSuitNumLabel.text=[NSString stringWithFormat:@"%ld",self.ResultModel.data.notSuitableNums];
        
        return cell;
        
        
    }else{
        
        ResultoptionTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell4" forIndexPath:indexPath];
 
        
        if (self.enterWay==0&&[self getUserType]==2) {
            //检查员才存在
            
            cell.ishidenSearh=NO;
            
        }else{
            
            cell.ishidenSearh=YES;
            
        }
        
        
        
        if ([self getUserType]==2&&!self.ResultModel.data.confirmState&&(self.ResultModel.data.checkUserId==[LoginStatuesModel getStaffId])) {
            
            //检查员未确认状态下才有
            cell.isHidenSureBtn=NO;
            
        }else{
            
            cell.isHidenSureBtn=YES;
            
        }

        cell.block=^(NSInteger num)
        {
            switch (num) {
                case 1:
                {
                    //不符合项
                    
                    CheckUnPassViewController *unpass=[CheckUnPassViewController new];
                    
                    unpass.resultId=self.resultId;
                    
                    unpass.OpenLookOther=self.OpenLookOther;
                    
                    unpass.confirmState=self.ResultModel.data.confirmState;
            
                    [self.navigationController pushViewController:unpass animated:YES];
                    
                }
                    break;
                    
                case 2:
                {
                    //确认检查
                    PtPeopleView *pt=[[PtPeopleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) andBlock:^(NSString *people,NSString*EscortInspector,NSString *date) {
                       
                        [self gotoSureCheck:date andjuror:people andEscortInspector:EscortInspector];
                        
                    }];
                    
                    
                    pt.ClickBlock=^(UITextField *textF)
                    {
                        
                        [self.view endEditing:YES];
                        
                        DatePickView *datePick=[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDevice_Is_iPhoneX?SCREEN_HEIGHT-64-IPhoneXTableHeight: SCREEN_HEIGHT-64) andDate:[NSDate date] andblock:^(NSDate *PickDate) {
                      
                      
                            textF.text=[HelpWay dateToStringWith:PickDate];
                            
                        }];
                        
                        [datePick PopWithView:self.view];
                        
                    };
           
                    [pt popWithSuperView:self.view];
                    
                }
                    break;
                case 3:
                {
                   //返回搜索
                    
                    [kAppDelegate initUserManager];
  
                }
                    break;
                case 4:
                {
                    //查看详情
                    
                    if ([self getUserType]==2&&self.enterWay==0) {
                        
                        [self.navigationController popViewControllerAnimated:YES];
                        
                    }else{
                        
                        NewSubjectData *data=[NewSubjectData new];
                        
                        data.viewType=self.ResultModel.data.viewType;
                        
                        data.subjectId=self.ResultModel.data.subjectId;
                        
                        data.resultId=[NSString stringWithFormat:@"%ld",self.ResultModel.data.resultId];
                        
                        //进入检查表
                        if (self.ResultModel.data.viewType==1) {
                            
                            //多页
                            
                            MorePageSubViewController *morePage=[MorePageSubViewController new];
                            
                            morePage.SubModel=data;
                            
                            morePage.isPop=YES;
                            
                            morePage.confirmState=self.ResultModel.data.confirmState;
                     
                            morePage.OpenLookOther=self.OpenLookOther;
                            
                            morePage.title=self.TipTitle;
                            
                            if (self.ResultModel.data.checkUserId==[LoginStatuesModel getStaffId]) {
                                
                                morePage.CheckLookSelfRecord=YES;
                                
                            }else{
                                morePage.CheckLookSelfRecord=NO;
                                
                            }
                            
                            [self.navigationController pushViewController:morePage animated:YES];
                            
                        }else{
                            
                            //单页
                            
                            SinglePageSubViewController *singlePage=[SinglePageSubViewController new];
                            
                            singlePage.SubModel=data;
                            
                            singlePage.ContentUrl=@"/index.php/FoodCheckApi/inspectSinglePageItemList.json";
                            
                            singlePage.isPop=YES;
                            
                            singlePage.title=self.TipTitle;
                            
                            singlePage.confirmState=self.ResultModel.data.confirmState;
                            
                              singlePage.OpenLookOther=self.OpenLookOther;
                            
                            if (self.ResultModel.data.checkUserId==[LoginStatuesModel getStaffId]) {
                                
                                singlePage.CheckLookSelfRecord=YES;
                                
                            }else{
                                singlePage.CheckLookSelfRecord=NO;
                                
                            }
                            
                            [self.navigationController pushViewController:singlePage animated:YES];
                            
                            
                        }
                        
                    }
                    
                }
                    break;
                    
                default:
                    break;
            }
            
        };
        
        return cell;
 
    }

}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        return 60;
        
    }else if (indexPath.section==1)
    {
        
        return 100;
        
    }else if (indexPath.section==2)
    {
        return 180;
        
    }else{
        
        if ([self getUserType]==2&&self.ResultModel.data.confirmState) {
         
            return 126;
            
        }
        
        return 180;
        
    }

}

-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectResultReport.json" andrequestTime:5 andParams:@{@"resultId":self.resultId} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        self.ResultModel=[CheckResultModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
   
            [self.hometableView reloadData];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
             self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
   
    }];
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==3) {
        
        return 0;
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0;
}

//提交确认检查
-(void)gotoSureCheck:(NSString*)orderRectifyDate andjuror:(NSString*)juror andEscortInspector:(NSString*)EscortInspector
{
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    
    [dict setValue:orderRectifyDate forKey:@"orderRectifyDate"];
    
    [dict setValue:juror forKey:@"juror"];
    
    [dict setValue:EscortInspector forKey:@"checkJuror"];
    
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectResultConfrim.json" andrequestTime:5 andParams:@{@"resultId":self.resultId,@"postData":[HelpWay dictionaryToJSON:dict]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];

        if (responseStatus==1) {
            //请求成功
            
            [MBProgressHUD showSuccessMessage:@"确认成功"];
            
            [self loadDatas];
            
            KPostNotification(@"StateChange", nil);
            
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
     
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
    }];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
